//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_no=thisform.find('input[name=goods_no]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var bar_code=thisform.find('input[name=bar_code]').val();
		$('#indexgoodsDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#indexgoodsDatagrid').datagrid({ 
			queryParams:{
				goods_no:goods_no,
				goods_name:goods_name,
				bar_code:bar_code
			}
		});
	})
	//更新行数据
	function moveDepotInfoUpdateActions(value){
		$.post(moveDepotInfoEditUrl,{
									id		:value.id,
									nums	:value.nums
								},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#moveDepotInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#moveDepotInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#moveDepotInfoDatagrid').datagrid('selectRow', index);
				$('#moveDepotInfoDatagrid').datagrid('endEdit', editIndex);
				$('#moveDepotInfoDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#moveDepotInfoDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#moveDepotInfoDatagrid').datagrid({
		//url:getarrivalUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getmoveDepotInfoUrl,
		title:'调拨单详情',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'unit1',title:'单位',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getUnitUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.unit_name;
				}
		},
		{field:'nums',title:'数量',sortable:true,
				editor: {
					type: 'numberbox',  
					options: {
						required: true,  
						missingMessage: '不能为空',
						min:1
					}
				}
		},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'manua_id',title:'账册编码',sortable:true},
		{field:'goods_name',title:'商品名称',sortable:true},
		{field:'goods_spec',title:'规格',sortable:true},
		{field:'current_type',title:'币制',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCoinUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.coin_name;
				}
		},
		{field:'country_code',title:'原产国',sortable:true,
				formatter:function(value,row,index){
					htmlobj=$.ajax({type:'post',url:getCountryUrl,data:{code:value},async:false});//ajax动态获取erp的名称
					var info=eval('('+htmlobj.responseText+')');
					return info.country_name;
				}
		}
		]],
		queryParams:{
			order_id:order_id
		},
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					moveDepotInfoUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
indexgoodsObj={
	showAll:function(){
		$('#indexgoodsDatagrid').datagrid({
				queryParams:''
		});
		$('.datagrid-pager-form').find('.datagrid-pager').css({'position':'relative','bottom':'0'});
	},
	save:function(url){
		$('#passwordsetForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
				var result=eval('('+result+')');
            	$.messager.confirm('提示',result.message,function(r){
                    	$('#passwordsetDialog').dialog('close');
                });
            }
        });
	},
	passwordsetBar:function(){
		$('#passwordsetDialog').dialog('open');
		$('#passwordset_username').textbox('setValue',username);
	},
	addBar:function(status,url){
		// alert(123);
		var addgoodsUrl=url;
		// alert(addgoodsUrl);
		var indexgoodsRow=$('#indexgoodsDatagrid').datagrid('getSelections');
        if (indexgoodsRow.length>0){
        	$.messager.confirm('提示','真的要添加这'+indexgoodsRow.length+'个商品吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<indexgoodsRow.length;i++){
                		ids.push(indexgoodsRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(addgoodsUrl,{id:id,order_id:order_id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#indexgoodsAddDialog').dialog('close');
								if(status=='moveDepotInfo'){
									$('#moveDepotInfoDatagrid').datagrid('reload'); 
								}
								if(status=='otherOutboundInfo'){
									$('#otherOutboundInfoDatagrid').datagrid('reload'); 
								}
								
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	addinfoBar:function(status,url){
		
		 $('#indexgoodsAddDialog').dialog({
			//minimizable: true,//最小化，默认false  
			//maximizable: true, //最大化，默认false  
			//collapsible: true, //可折叠，默认false
			//按钮  
            buttons: [{
                text: '确定添加',
                iconCls: 'icon-ok',
                handler: function () {
					indexgoodsObj.addBar(status,url);
                }
            }, {
                text: '取消',
                handler: function () {
                    $('#indexgoodsAddDialog').dialog('close');
                }
            }]
		});
        $('#indexgoodsAddDialog').dialog('open').dialog('setTitle','商品列表');
		$('#indexgoodsDatagrid').datagrid({
			url:getgoodsUrl,
			fitColumns:true,
			rownumbers:true,
			pageSize:25,
			pageNumber:1,
			pageList:[1,2,5,10,15,20,25,30,40,50],
			columns:[[
			{field:'id',title:'id',checkbox:true},
			{field:'goods_no',title:'商品编码',sortable:true},
			{field:'manual_id',title:'账册编码',sortable:true},
			{field:'goods_name',title:'商品名称',sortable:true},
			{field:'bar_code',title:'条形码',sortable:true},
			{field:'goods_spec',title:'规格',sortable:true},
			{field:'storage_amount',title:'库存数量',sortable:true},
			{field:'order_no',title:'单号',sortable:true},
			{field:'batch_no',title:'批次号',sortable:true}
			]],
			onLoadError:function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					var tab=$('#Tabs').tabs('getSelected');
					var index=$('#Tabs').tabs('getTabIndex',tab);
					$('#Tabs').tabs('close',index);
				});
			},
			pagination:true,
			fit:true,
		});
		$('.datagrid-pager-form').find('.datagrid-pager').css({'position':'relative','bottom':'0'});
	}
}
moveDepotInfoObj={
	save:function(url){//编辑后保存数据
		$('#moveDepotInfoEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#moveDepotInfoEditDialog').dialog('close');
                    });
                }else{
                    $('#moveDepotInfoEditDialog').dialog('close');      
                    $('#moveDepotInfoDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#moveDepotInfoDatagrid').datagrid('validateRow', editIndex)){
			$('#moveDepotInfoDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#moveDepotInfoDatagrid').datagrid('acceptChanges');
		}
	},
	reloadBar:function(){//刷新数据
		$('#moveDepotInfoDatagrid').datagrid('reload');
	},
	editBar:function(){//显示编辑框
		var moveDepotInfoRow=$('#moveDepotInfoDatagrid').datagrid('getSelections');
        if (moveDepotInfoRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(moveDepotInfoRow.length==1){            	
        	$('#moveDepotInfoEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#moveDepotInfoEditForm').form('load',moveDepotInfoRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var moveDepotInfoRow=$('#moveDepotInfoDatagrid').datagrid('getSelections');
        if (moveDepotInfoRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+moveDepotInfoRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<moveDepotInfoRow.length;i++){
                		ids.push(moveDepotInfoRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(moveDepotInfoDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#moveDepotInfoAddDialog').dialog('close');       
								$('#moveDepotInfoDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}